package com.ct.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ct.model.vo.UploadEventListVo;
import com.ct.model.vo.UploadEventVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 测试Controller
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private HttpServletRequest request;

    /**
     * 测试层
     */
    @GetMapping("/to")
    public String test() {
        Console.log("requestURI:{}", request.getRequestURI());
        log.info("requestURI:{}", request.getRequestURI());
        return "success";
    }


    /**
     * get日志测试层
     */
    @GetMapping("/get")
    public List get(String param) {
        Console.log("requestURI:{}", request.getRequestURI());
        log.info("token:{}", request.getHeader("token"));
        log.info("param:{}", param);
        log.info("requestURI:{}", request.getRequestURI());
        log.info("本地IP:{}", ServletUtil.getClientIP(request));
        // ThreadUtil.sleep(5000);
        ArrayList<Map> mapArrayList = new ArrayList<>();
        mapArrayList.add(MapUtil.builder(new HashMap<String, Object>()).put("filaNo", 1).put("groupNo", "0").put("lineCode", "210").build());
        mapArrayList.add(MapUtil.builder(new HashMap<String, Object>()).put("filaNo", 1).put("groupNo", "0").put("lineCode", "X205D").build());
        return mapArrayList;

    }

    /**
     * post日志测试层
     */
    @PostMapping("/post")
    public String post(@RequestBody Map map) {
        Console.log("requestURI:{}", request.getRequestURI());
        log.info("token:{}", request.getHeader("token"));
        log.info("map:{}", map);
        log.info("requestURI:{}", request.getRequestURI());

        ThreadUtil.sleep(5000);
        return JSONUtil.toJsonStr(map);
    }

    @PostMapping("/isValid")
    public String isValid(@RequestBody String param) {
        Console.log("a:{}",param);
        if (StrUtil.isNotBlank(param) && JSON.isValidArray(param)){
            JSONArray array = JSON.parseArray(param);
            Console.log("arry:{}",array);
        }
        return Convert.toStr(StrUtil.isNotBlank(param) && JSON.isValidArray(param));
    }


    @PostMapping("/uploadEvent")
    public String uploadEvent( @RequestBody @Valid UploadEventListVo uploadEventVos) {
        for (UploadEventVo event : uploadEventVos.getEvents()) {
            Console.log("aaaa:{}",JSONUtil.toJsonStr(event));
        }

        Console.log("uploadEventVos:{}",uploadEventVos.getEvents());
        Console.log("uploadEventVos:{}",JSONUtil.toJsonStr(uploadEventVos.getEvents()));
        return "sussess";
    }
    }
